package view;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import model.OraConnection;
import model.Surat;

public class PanelDaftarSurat extends JPanel {

    private JTable tableSurat;
    private JLabel labelPanel;
    private ArrayList<Surat> data;

    public PanelDaftarSurat() {
    }

    public PanelDaftarSurat(ArrayList<Surat> data) {
        this.data = data;
        setLayout(new BorderLayout());

        labelPanel = new JLabel("Form Daftar Surat");
        labelPanel.setHorizontalAlignment(JLabel.CENTER);
        add(labelPanel, BorderLayout.NORTH);

        tableSurat = new JTable();
        tableSurat.setModel(new TabelSurat(data));
        add(tableSurat, BorderLayout.CENTER);
        tableSurat.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                try {
                    int surat = tableSurat.rowAtPoint(e.getPoint());
                    String id = tableSurat.getValueAt(surat, 0).toString();
                    String no = String.valueOf(id);
                    JOptionPane.showMessageDialog(null, "surat sudah diperiksa");
                    Connection conn = OraConnection.open("jdbc:oracle:thin:@172.23.9.185:1521:orcl", "MHS115314063", "MHS115314063");
                    Statement statement = conn.createStatement();
                    String sql = "UPDATE surat SET ket = 'Diterima' WHERE NO_SURAT = "+no;
                    statement.executeQuery(sql);
                } catch (SQLException ex) {
                    Logger.getLogger(PanelDaftarSurat.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });
    }

    public ArrayList<Surat> getData() {
        return data;
    }

    public void setData(ArrayList<Surat> data) {
        this.data = data;
    }
}
